package acwing.算法题;

import jdk.internal.util.xml.impl.Pair;

import java.util.*;

/**
 * @author： li
 * @date： 2022-03-26 18:45
 * @version 1.0
 */

public class map键值对 {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        //增强for循环遍历
        //使用keySet()遍历
        for (String key : map.keySet()) {
            System.out.println(key + " ：" + map.get(key));
        }
//        使用entrySet()遍历
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry.getKey() + " ：" + entry.getValue());
        }
//        迭代器遍历
//        使用keySet()遍历
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            System.out.println(key + "　：" + map.get(key));
        }
//        使用entrySet()遍历
        Iterator<Map.Entry<String, String>> iterator1 = map.entrySet().iterator();
        while (iterator1.hasNext()) {
            Map.Entry<String, String> entry = iterator1.next();
            System.out.println(entry.getKey() + "　：" + entry.getValue());
        }
    }
}
